<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="ActivityName" label="活动名称">
				<uni-data-picker v-model="formData.ActivityName" collection="abu_activity"
					field="ActivityName as text,_id as value" trim="both"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="community_id" label="社区名称">
				<uni-data-picker v-model="formData.community_id" collection="abu_community"
					field="name as text,_id as value" trim="both"></uni-data-picker>
			</uni-forms-item>
			<!-- 			<uni-forms-item name="user_id" label="用户昵称">
				<uni-data-picker v-model="formData.user_id" collection="uni-id-users"
					field="username as text, _id as value"></uni-data-picker>
			</uni-forms-item> -->
			<uni-forms-item name="Name" label="真实姓名" required>
				<uni-easyinput placeholder="参与者的真实姓名" v-model="formData.Name" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="Contact" label="联系方式" required>
				<uni-easyinput placeholder="联系方式，可以是电话号码或电子邮箱" v-model="formData.Contact" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="RegistrationStatus" label="报名状态" v-show="false">
				<uni-data-checkbox v-model="formData.RegistrationStatus"
					:localdata="formOptions.RegistrationStatus_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<!-- 			<uni-forms-item name="Notes" label="备注">
				<uni-easyinput placeholder="关于参与者或报名的额外信息或特殊需求" v-model="formData.Notes" trim="both"></uni-easyinput>
			</uni-forms-item> -->
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/abu_baoming.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'abu_baoming';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}
	export default {
		async onLoad(e) {
			if (e && e.id) {
				this.id = e.id;
				// uni.hideTopWindow()
				this.formData.ActivityName = e.id;
				// this.formData.user_id = await uniCloud.getCurrentUserInfo().uid
				const community = await uniCloud.database().collection('abu_community').where({
					weiyi: e.weiyi
				}).get({
					getOne: true
				})
				console.log(community);
				this.formData.community_id = community.result.data._id
				this.formData.baoming_type = 1;
				this.formData.RegistrationStatus = 1;
			}
		},
		data() {
			let formData = {
				"ActivityName": "",
				"community_id": "",
				"user_id": "",
				"Name": "",
				"Contact": "",
				"RegistrationStatus": null,
				"Notes": ""
			}
			return {
				formData,
				formOptions: {
					"RegistrationStatus_localdata": [{
							"text": "已报名",
							"value": 0
						},
						{
							"text": "待审核",
							"value": 1
						},
						{
							"text": "已取消",
							"value": 2
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 检查一下是否已经存在

				db.collection("abu_baoming").where({
					Contact: value.Contact
				}).get({
					getOne: true
				}).then(item => {
					if (item.result.data && item.result.data.Contact) {
						value.scan = true
					}
				})


				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).where({
					Contact: value.Contact
				}).update(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>